<template>
  <el-dialog
    :title="'修改密码'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" :min="6" placeholder="请输入新密码"></el-input>
    </el-form-item>
    <el-form-item label="确定密码" prop="repassword">
      <el-input v-model="dataForm.repassword" placeholder="请再次确定密码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import md5 from "js-md5";
  export default {
    data () {
       var validateConfirmPassword = (rule, value, callback) => {
        if (this.dataForm.password !== value) {
          callback(new Error("确认密码与新密码不一致"));
        } else {
          callback();
        }
      };
      return {
        isdisabled:false,
        visible: false,
        dataForm: {
          id: 0,
          password: '',
          repassword: ''
        },
        dataRule: {
         password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { min: 6, max: 18, message: '长度在 6到 18个位', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validateConfirmPassword, trigger: "blur" },
        ],
        
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
       
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isdisabled=true
            this.$http({
              url: this.$http.adornUrl(`/admin/sys/user/password`),
              method:'POST',
              data: this.$http.adornData({
                  "newPassword":md5(this.dataForm.password).toLowerCase(),
                  "userId":this.dataForm.id
              })
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.isdisabled=false
            })
          }
        })
      }
    }
  }
</script>
